<?php

namespace App\Logics\Api\Ucenter;

use App\Commons\Domain\Module\System\Model\Country;
use App\Commons\Domain\Module\UserCenter\Contract\AccountService;
use App\Commons\Domain\Module\UserCenter\Model\Member;
use App\Commons\Domain\Module\UserCenter\Model\UserLog;
use App\Commons\Libs\SmsVerifycode;
use App\Services\Java\JavaService;
use GuzzleHttp\Exception\GuzzleException;
use Hash;

/**
 *
 * @authors generator
 * @date    2020-10-19 16:51:40
 */
class UserLogic
{
    /**
     * 用户注册
     * @param array $param
     * @return mixed
     * @throws GuzzleException
     */
    public function register(array $param)
    {
        throw_on($param['password'] !== $param['password_repeat'], 0xf00132);

        $username = empty($param['mobile']) ? $param['email'] : $param['mobile'];

        # TODO:验证code

        $user = app(AccountService::class)->createUser(
            $username,
            $param['password'],
            $param['mobile'],
            $param['country_id'],
            $param['email']
        );

        # TODO:创建用户钱包
//        app(JavaService::class)->createWallet($user->uid);
        return $user;
    }

    /**
     * 用户登录
     * @param array $param
     * @param string $ip
     * @return array
     * @throws \Exception
     */
    public function login(array $param, string $ip)
    {
        $param = collect($param)->filter(fn($item) => $item)->toArray();
        if (!$token = auth()->attempt($param)) {
            throw_e(0xf00142);
        }

        app(AccountService::class)->userLogin(auth()->user(), $ip);

        return compact('token');
    }

    /**
     * 获取用户信息
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function info()
    {
        return app(AccountService::class)->userInfo(auth()->user());
    }

    /**
     * 修改用户信息
     * @param array $param
     * @return mixed
     */
    public function updateInfo(array $param)
    {
        return app(AccountService::class)->updateUserInfo(
            auth()->user(),
            $param['nickname'],
            $param['avatar'],
            $param['gender']
        );
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        $token = auth()->getToken()->get();
        app(AccountService::class)->userLogout($token);
        auth()->invalidate($token);
    }

    /**
     * 重置密码
     * @param array $param
     * @return mixed
     */
    public function reset(array $param)
    {
        throw_on($param['password'] !== $param['password_repeat'], 0xf00132);

        $username = empty($param['mobile']) ? $param['email'] : $param['mobile'];

        # TODO:验证code

        return app(AccountService::class)->forgotPassword($param['mobile'], $param['password'], $param['email']);
    }

    /**
     * 修改密码
     * @param array $param
     * @return mixed
     */
    public function updatePwd(array $param)
    {
        $user = auth()->user();

        throw_on(!Hash::check($param['old_password'], $user->account->password), 0xf00172);
        throw_on($param['password'] !== $param['password_repeat'], 0xf00132);

        return app(AccountService::class)->updatePassword($user, $param['password']);
    }

    /**
     * 获取操作记录
     * @param array $params
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getLogs(array $params)
    {
        return UserLog::whereType($params['type'])->latest()
            ->paginate($params['limit']);
    }

    /**
     * 修改通知状态
     * @param string $field
     * @param int $status
     * @return mixed
     */
    public function switchNotice(string $field, int $status)
    {
        return app(AccountService::class)->switchNotice(auth()->user(), $field, $status);
    }

    /**
     * 发送验证码
     * @param array $param
     * @return string
     */
    public function code(array $param)
    {
        if (!empty($param['mobile'])) {
            # TODO:发送手机验证码
            return '手机验证码已发送';
        } else {
            # TODO:发送邮箱验证码
            return '邮箱验证码已发送';
        }
    }

    /**
     * 国家列表
     * @return \Illuminate\Support\Collection
     */
    public function country()
    {
        return Country::select(['id', 'zh_name', 'en_name', 'area_code'])->orderBy('sort_num')->get();
    }

    /**
     * 通过验证码设置资金密码
     * @param array $params
     * @return mixed
     */
    public function setPwd(array $params)
    {
        # TODO: 验证验证码
        return app(AccountService::class)->changePwd(auth()->user(), $params['password'],
            $params['password_repeat']);
    }

    /**
     * 通过旧密码重设资金密码
     * @param array $params
     * @return mixed
     */
    public function changePwd(array $params)
    {
        $user = Member::whereUid(auth()->user()->uid)->first();

        throw_on(!Hash::check($params['original_password'], $user->jy_password), 0xf00172);

        return app(AccountService::class)->changePwd(auth()->user(), $params['password'],
            $params['password_repeat']);
    }
}
